#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int value;
struct node *next;
};
struct node *first= NULL;

// program to append in the linking list
void append(struct node *list, int x)
{
struct node *new_node;
new_node=malloc(sizeof(struct node));
if(list==NULL)
{
new_node->value=x;
new_node->next=NULL;
first=new_node;
}
else
{
while(1)
{
if(list->next==NULL)
{
new_node->value=x;
new_node->next=NULL;
list->next=new_node;
break;
}
list=list->next;
}
}
}

// program to insert at a given position in the list
void insert(struct node *list, int in,int x)
{
int count=0;
struct node *new_node;
new_node=malloc(sizeof(struct node));
if(in==0)
{
new_node->value=x;
new_node->next=list;
first=new_node;
}
else
{
while(list!=NULL)
{	if(count==in-1)
		break;
	list=list->next;
	count++;
	
}
new_node->value=x;
new_node->next=list->next;
list->next=new_node;
}
}
//program to delete element from the list
void delete(struct node *list, int n)
{
  struct node *cur,  *prev;
	  
  for ( cur=list,prev = NULL;
       cur != NULL && cur->value != n;
       prev = cur, cur = cur->next)
    ;
  if(cur==NULL)
     printf("n value is not in the linked list \n");
   else            
 {
 if (prev == NULL)
    list = list->next; 
      
  else
    prev->next = cur->next;   
  free(cur);
}
  
}

// program to pop element from the list at a given position
void pop1(struct node *list, int n)
{
  int count;
  struct node *cur,  *prev;
	  
  for ( cur=list,prev = NULL,count=0;
       cur != NULL && count != n;
       prev = cur, cur = cur->next,count++)
    ;
  if(cur==NULL)
     printf("index out of range \n");
   else            
 {
 if (prev == NULL)
    list = list->next; 
      
  else
    prev->next = cur->next;   
  free(cur);
}
  
}

// program to display elements of the linking list
struct node *display(struct node *list)
{
printf("the linked list is as follows \n");
while(list!=NULL)
{
printf("%d\n",list->value);
list=list->next;
}
}

/*struct node *add_to_list(struct node *list, int n)
{
struct node *new_node;
	 
new_node = malloc(sizeof(struct node));
 if (new_node == NULL) {
printf("Error: malloc failed in add_to_list\n");
exit(EXIT_FAILURE);
 }
new_node->value = n;
new_node->next = list;
return new_node;
}
struct node *read_numbers(void)
{
int n;
	 
 printf("Enter a series of integers (0 to terminate): ");
for (;;) 
{
 scanf("%d", &n);
 if (n == 0)
return first;
first = add_to_list(first, n);
 }
}*/

int main()
{
int x,n,l,in;
printf("enter the value  u want to store in the linked list(enter 0 to terminate) \n");
scanf("%d",&n);

while(1)
{
	if(n==0)
	break;
	struct node *new_node1;
	new_node1=malloc(sizeof(struct node));
	new_node1->value=n;
	new_node1->next=first;
	first=new_node1;
	printf("enter the value  u want to store in the linked list(enter 0 to terminate) \n");
	scanf("%d",&n);
	
}
display(first);
printf("enter 0 to append, 1 to delete,2 to pop,3 to insert \n");
scanf("%d",&l);
switch(l)
{
case 0:
printf("enter the value u want to append \n");
scanf("%d",&x);
append(first,x);
display(first);
break;

case 1:
printf("enter the value u want to delete \n");
scanf("%d",&x);
delete(first,x);
display(first);
break;

case 2:
printf("enter the position at which u want to pop \n");
scanf("%d",&x);
pop1(first,x);
display(first);
break;

case 3:
printf("enter the position at which u want to insert \n");
scanf("%d",&in);
printf("enter the value u want to insert \n");
scanf("%d",&x);
insert(first,in,x);
display(first);

}
}

